﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XChip_Common;
using BinAnalysisHelp;
using System.Configuration;

namespace XChipTool
{
    public partial class XChip_BinPassWord : DevExpress.XtraEditors.XtraForm
    {
        public XChip_BinPassWord()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 设置密码框显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShowPwd_Click(object sender, EventArgs e)
        {
            passwordText.Properties.UseSystemPasswordChar = (btnShowPwd.Tag.ToString()=="0"?false:true);
            btnShowPwd.Tag = (passwordText.Properties.UseSystemPasswordChar ? "0" : "1");
            btnShowPwd.ImageOptions.ImageIndex = passwordText.Properties.UseSystemPasswordChar ? 1 : 0;
        }
        /// <summary>
        /// 保存密钥
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                
                AppHelper.UpdateAppConfig("BinSecretKey", passwordText.Text);
                DevExpress.XtraEditors.XtraMessageBox.Show("修改成功！");
            }
            catch (Exception)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("修改失败！");
                throw;
            }
            
        }

        /// <summary>
        /// 初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XChip_BinPassWord_Load(object sender, EventArgs e)
        {
          passwordText.Text =  AppHelper.GetAppConfig("BinSecretKey");
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            var vual = AppHelper.XorNotEncryption(passwordText.Text).ToString("x4");
            bool isbool = false;
            if (GetConfigValue("IsSerial") == "USB" || GetConfigValue("IsI2C") == "1")
            {
                if (null != BinHelp.usbList)
                {
                    isbool = BinHelp.SetBinUpdateData(ref BinHelp.BinList, "BIN密码的地址", vual);
                }
            }
            else
            {
                if (null != BinHelp.lspList)
                {   //编写Bin更新数据

                    isbool = BinHelp.SetBinUpdateData(ref BinHelp.BinList, "BIN密码的地址", vual);
                }
            }
            if (isbool)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("修改成功！");
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("修改失败，请检测是否开启bin进bin出功能！");
            }
           
        }
        private string GetConfigValue(string strKey)
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var settings = configFile.AppSettings.Settings;

            return settings[strKey].Value;
        }
    }
}